package com.yrmao.chiefenergyofficer.ui.fragment.itemcontent;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.yrmao.chiefenergyofficer.ui.modle.GridviewContentInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yemao on 16/11/30.
 */

public class ContentPageAdapter extends PagerAdapter {
    private List<GridviewContentInfo> list;
    private Context context;
    private int allpage = 1;//总页数
    private int pagesize = 4;

    public ContentPageAdapter(List<GridviewContentInfo> list, int pagesize, Context context) {
        this.list = list;
        this.context = context;
        this.pagesize = pagesize;
        if (list != null)
            if (list.size() % pagesize >= 1) {
                allpage = list.size() / pagesize + 1;
            } else {
                allpage = list.size() / pagesize;
            }
    }

    @Override
    public int getCount() {
        return allpage;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        List<GridviewContentInfo> data = new ArrayList<>();

        for (int i = position * pagesize; i < (position + 1) * pagesize; i++) {//每页4个
            if (i >= list.size())
                break;
            data.add(list.get(i));
        }
        ContentGridLayout gridLayout = new ContentGridLayout(context, data, pagesize);
        container.addView(gridLayout);
        return gridLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
